package com.lemt.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.Update;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.lemt.common.mapper.AddressMapper;
import com.lemt.common.pojo.Address;
import com.lemt.mall.entity.*;
import com.lemt.mall.entity.vo.OrderVo;
import com.lemt.mall.entity.vo.ShoppingCartVo;
import com.lemt.mall.mapper.*;
import com.lemt.mall.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Or;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zik
 * @since 2023-09-10
 */
@Slf4j
@Service(value = "OrdersService")
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {


    @Resource
    private OrderGoodsMapper orderGoodsMapper;

    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    @Resource
    private AddressMapper addressMapper;


    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private InventoryMapper inventoryMapper;

    @Resource
    private OrdersMapper ordersMapper;

    @Override
    public Boolean addOrder(OrderVo orderVo) {
        log.info("订单vo->{}",orderVo);
        log.info("生成一个UUID作为标识");
        String orderNum = UUID.randomUUID().toString().replace("-", "");

        log.info("获取到购物车vo");
        List<ShoppingCartVo> shoppingCartVos = orderVo.getShoppingCartVos();

        shoppingCartVos.forEach(item -> {
            log.info("item ->> {}",item );

            log.info("便利购物车vo->{}",item);
            Inventory inventory = item.getInventory();

            LambdaUpdateWrapper<Inventory> updateWrapper = new LambdaUpdateWrapper<Inventory>()
                    .set(Inventory::getInventory, inventory.getInventory() - item.getCount())
                    .eq(Inventory::getId, item.getInventory().getId());
            log.info("修改对应库存");
            if (inventoryMapper.update(inventory,updateWrapper) == 1){
                log.info("修改库存成功");
            }

            shoppingCartMapper.deleteById(item.getId());
            log.info("购物车表数据已删除");

            OrderGoods orderGoods = new OrderGoods();
            orderGoods.setOrderNum(orderNum);
            orderGoods.setCartId(item.getGoods().getId());
            orderGoods.setUserId(item.getUserId());

            orderGoodsMapper.insert(orderGoods);
            log.info("将订单表中的购物车数据添加到对照表中。");
        });

        Address address = orderVo.getAddress();

        Orders orders = new Orders();
        orders.setOrderNum(orderNum);
        orders.setAddressId(address.getId());
        orders.setUserId(orderVo.getUser_id());
        orders.setIsPlay(orderVo.getIsPay());

        if (ordersMapper.insert(orders) == 1){
            return true;
        }

        return false;
    }

    @Override
    public Boolean removeOrder(String orderNum) {
        // 取消订单
        log.info("获取对照表中的数据");
        List<OrderGoods> orderGoods = orderGoodsMapper.selectList(new LambdaQueryWrapper<OrderGoods>().eq(OrderGoods::getOrderNum, orderNum));
        orderGoods.forEach(item -> {
            Integer cartId = item.getCartId();
            log.info("根据对照表请求到原购物车中的数据");
//            ShoppingCart shoppingCart = shoppingCartMapper.selectOne(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getId, cartId).eq(ShoppingCart::getIsDelete, 1));
            ShoppingCart shoppingCart = shoppingCartMapper.selectDelete(cartId, 1);

            log.info("请求到的数据为{}",shoppingCart);

            int i = shoppingCartMapper.reBackCart(item.getCartId());
            System.out.println(i);
            log.info("恢复购物车数据{}",shoppingCart);
            Integer inventoryId = shoppingCart.getInventoryId();
            log.info("恢复库存");
            Inventory inventory = inventoryMapper.selectOne(new LambdaQueryWrapper<Inventory>().eq(Inventory::getId, inventoryId));

            inventoryMapper.update(inventory ,new LambdaUpdateWrapper<Inventory>().set(Inventory::getInventory,inventory.getInventory()+shoppingCart.getCount()));
            log.info("删除对照表");
            orderGoodsMapper.delete(new LambdaQueryWrapper<OrderGoods>().eq(OrderGoods::getOrderNum,orderNum));
        });
        log.info("删除订单数据");
        ordersMapper.delete(new LambdaQueryWrapper<Orders>().eq(Orders::getOrderNum,orderNum));
        return true;
    }

    @Override
    public List<OrderVo> getOrder(Integer id) {
        log.info("根据用户id获取订单");
        List<OrderVo> ordersVos = new ArrayList<>();
        List<Orders> orders = ordersMapper.selectList(new LambdaQueryWrapper<Orders>().eq(Orders::getUserId, id));
        log.info("订单数据为{}",orders);
        orders.forEach(order -> {

            OrderVo orderVo = new OrderVo();

            // 添加库存信息
            Address address = addressMapper.selectOne(new LambdaQueryWrapper<Address>().eq(Address::getId, order.getAddressId()));
            log.info("address->{}",address);
            orderVo.setAddress(address);
            orderVo.setIsPay(order.getIsPlay());
            orderVo.setUser_id(order.getUserId());
            log.info("添加基本信息");
            List<OrderGoods> orderGoods = orderGoodsMapper.selectList(new LambdaQueryWrapper<OrderGoods>().eq(OrderGoods::getOrderNum, order.getOrderNum()));

            log.info("开始添加购物车信息");
            List<ShoppingCartVo> shoppingCartVos = new ArrayList<>();

            orderGoods.forEach(orderGood -> {
//                List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getId, orderGood.getCartId()));
                List<ShoppingCart> shoppingCarts = shoppingCartMapper.getListById(orderGood.getCartId());
                log.info("获取到对照表信息{}",shoppingCarts);
                shoppingCarts.forEach(shoppingCart -> {
                    ShoppingCartVo shoppingCartVo = new ShoppingCartVo();
                    Inventory inventory = inventoryMapper.selectOne(new LambdaQueryWrapper<Inventory>().eq(Inventory::getId, shoppingCart.getInventoryId()));
                    shoppingCartVo.setInventory(inventory);

                    Goods goods = goodsMapper.selectOne(new LambdaQueryWrapper<Goods>().eq(Goods::getId, shoppingCart.getGoodsId()));
                    shoppingCartVo.setGoods(goods);
                    shoppingCartVo.setCount(shoppingCart.getCount());
                    shoppingCartVo.setUserId(shoppingCart.getUserId());
                    shoppingCartVo.setId(shoppingCart.getId());
                    shoppingCartVos.add(shoppingCartVo);
                });
            });
            orderVo.setShoppingCartVos(shoppingCartVos);
            ordersVos.add(orderVo);
        });

        return ordersVos;
    }
}
